package com.tencent.hummingbird.portal.mapper;

import com.tencent.hummingbird.portal.pojo.Question;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tencent.hummingbird.portal.pojo.User;
import com.tencent.hummingbird.portal.vo.AskVO;
import com.tencent.hummingbird.portal.vo.MyQuestionVO;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* <p>
    *  Mapper 接口
    * </p>
*
* @author yxb
* @since 2024-01-10
*/
@Repository
public interface QuestionMapper extends BaseMapper<Question> {

    @Select("select q.* " +
            " from question q" +
            " left join user_question uq on q.id=uq.question_id" +
            " where q.user_id=#{userId} or uq.user_id=#{userId}" +
            " order by q.createtime desc")
    List<Question> findTeacherQuestions(Integer userId);
    @Select("SELECT q.id,q.user_nick_name,q.title,uc.createtime \n" +
            "            FROM user_collect uc\n" +
            "            JOIN question q\n" +
            "            ON uc.question_id = q.id\n" +
            "            WHERE uc.user_id = #{userId}")
    List<MyQuestionVO> findCollectQuestion(Integer uesrId);

    @Select("SELECT q.id,q.title,q.createtime,q.user_nick_name\n" +
            "FROM question q\n" +
            "WHERE q.user_id = #{userId}")
    List<AskVO> findMyAsk(Integer userId);

    @Select("SELECT page_views FROM question WHERE id = #{questionId}")
    Integer getViewsById(Integer questionId);

    @Update("update question set page_views = #{views} where id = #{questionId}")
    Integer updateViewsById(Integer views,Integer questionId);

    @Select("select * from question where id = #{questionId}")
    Question getQuestionById(Integer questionId);

    @Select("select nickname,id from user where id in\n" +
            "(select user_id from user_question where question_id = #{questionId})")
    List<User> getTeacherById(Integer questionId);

    @Select("SELECT * \n" +
            "FROM question\n" +
            "ORDER BY page_views DESC" )
    List<Question> selectHostQuestion();

    @Update("update question set status=#{status} where " +
            " id=#{questionId}")
    int updateStatus(Integer questionId,Integer status);

    @Select("SELECT q.*\n" +
            "FROM user_question uq\n" +
            "JOIN question q\n" +
            "ON uq.question_id = q.id\n" +
            "WHERE uq.user_id = #{teacherId} AND delete_status = 0 AND q.status=#{status}")
    List<Question> selectWorks(Integer teacherId,Integer status);
}
